package com.kerbores.blog.module;

import java.util.Date;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.lang.Times;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;

import com.kerbores.blog.bean.Article;
import com.kerbores.blog.module.common.BaseModule;
import com.kerbores.blog.service.IArticleService;
import com.kerbores.blog.service.ICommentService;
import com.kerbores.utils.entries.Result;
import com.kerbores.utils.web.pager.Pager;

/**
 * @author 贵源 <br>
 *         create at 2014年10月21日
 */
@At("article")
public class ArticleModule extends BaseModule {
	@Inject
	private IArticleService articleService;
	@Inject
	private ICommentService commentService;

	@At("/list")
	@Ok("vel:blog/list.vm")
	public Result list(int page) {
		Pager<Article> pager = articleService.findByPage(_fixPage(page));
		pager.setUrl("/article/list");
		data.put("pager", pager);
		return Result.success(data).setTitle("文章列表");
	}

	@At("/listByDate")
	@Ok("vel:blog/list.vm")
	public Result listByDate(int page, Date date) {
		Pager<Article> pager = articleService.listByDateAndPage(date, _fixPage(page));
		pager.setUrl("/article/listByDate");
		pager.addParas("date", Times.format("yyyy-MM-dd", date));
		data.put("pager", pager);
		return Result.success(data).setTitle("文章列表");
	}
	
	@At("/search")
	@Ok("vel:blog/list.vm")
	public Result search(String key,int page) {
		Pager<Article> pager = articleService.search(key, _fixPage(page));
		pager.setUrl("/article/search");
		pager.addParas("key", key);
		data.put("pager", pager);
		return Result.success(data).addData("key", key).setTitle("文章列表");
	}

	@At("/detail/?")
	@Ok("vel:blog/detail.vm")
	public Result detail(int id) {
		data.put("article", articleService.get(id));
		data.put("comments", commentService.findByArticleId(id));
		return Result.success(data).setTitle("文章详情");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kerbores.blog.module.common.BaseModule#_getNameSpace()
	 */
	@Override
	public String _getNameSpace() {
		return "article";
	}
}
